package com.jhs.boot.config;

import com.jhs.boot.system.service.FeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

/**
 * @Classname FeignConfig
 * @Date 2025/8/18 12:27
 * @Created by zxy
 */
 @Configuration
 @Slf4j
public class FeignConfig {


    /**
     * 创建FeignService客户端 - 修复版本
     */
    public FeignService getFeignClient(String url) {
        try {
            // 确保URL以/结尾，避免路径拼接问题
            if (!url.endsWith("/")) {
                url = url + "/";
            }

            RestClient restClient = RestClient.builder()
                    .baseUrl(url)
                    // 添加默认配置避免空指针
                    .defaultHeader("Content-Type", "application/json")
                    .defaultHeader("Accept", "application/json")
                    .requestInterceptor((request, body, execution) -> {
                        log.debug("Feign请求: {} {}", request.getMethod(), request.getURI());
                        return execution.execute(request, body);
                    })
                    .build();

            RestClientAdapter adapter = RestClientAdapter.create(restClient);
            HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder()
                    .exchangeAdapter(adapter)
                    .build();

            FeignService client = factory.createClient(FeignService.class);
            log.info("成功创建Feign客户端，URL: {}", url);
            return client;
        } catch (Exception e) {
            log.error("创建Feign客户端失败，URL: {}", url, e);
            throw new IllegalStateException("Feign客户端创建失败: " + url, e);
        }
    }


//    public FeignService  getFeignClient(String url){
//        RestClient restClient = RestClient.builder().baseUrl(url).build();
//        RestClientAdapter adapter = RestClientAdapter.create(restClient);
//        HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();
//        return factory.createClient(FeignService.class);
//    }
    /**
     * 创建HttpServiceProxyFactory的Builder
     */
//    @Bean
//    @Scope("prototype") // 每次获取都是新的实例
//    public HttpServiceProxyFactory.Builder httpServiceProxyFactoryBuilder() {
//        return HttpServiceProxyFactory
//                .builderFor(RestClientAdapter.create(RestClient.create()));
//    }
//
//    /**
//     * 创建FeignService客户端
//     */
//    public FeignService getFeignClient(String url) {
//        RestClient restClient = RestClient.builder()
//                .baseUrl(url)
//                .build();
//
//        RestClientAdapter adapter = RestClientAdapter.create(restClient);
//        HttpServiceProxyFactory factory = HttpServiceProxyFactory
//                .builderFor(adapter)
//                .build();
//
//        return factory.createClient(FeignService.class);
//    }




}
